/******************************************************************************
 * @file    main.c
 * @version V1.0
 * @date    2024-08-21
 * @brief   Main program entry
 *
 * @company WIZnet
 * @website https://wiznet.io/
 * @gitee  https://gitee.com/wiznet-hk
 * @github https://github.com/Wiznet/
 ******************************************************************************/

#include "bsp_rcc.h"
#include "bsp_tim.h"
#include "bsp_uart.h"
#include "delay.h"
#include "do_mqtt.h"
#include "wiz_interface.h"
#include "wizchip_conf.h"
#include "ws2812b.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Constant definitions */
#define SOCKET_ID             0
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)

/* Global variables */
wiz_NetInfo g_default_net_info =
    {
        .mac = {0x00, 0x08, 0xd0, 0x12, 0x22, 0x12},
        .ip = {192, 168, 1, 30},
        .gw = {192, 168, 1, 1},
        .sn = {255, 255, 255, 0},
        .dns = {8, 8, 8, 8},
        .dhcp = NETINFO_DHCP};

uint8_t g_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0};
uint8_t s_mqtt_send_buf[ETHERNET_BUF_MAX_SIZE] = {0};
uint8_t s_mqtt_recv_buf[ETHERNET_BUF_MAX_SIZE] = {0};

int main(void)
{
    /* Hardware initialization */

    rcc_clk_config();
    delay_init();
    console_usart_init(115200);
    tim3_init();

    printf("%s MQTT OneNET example\r\n", _WIZCHIP_ID_);

    /* LED strip initialization */
    ws2812b_init();
    ws2812b_update();

    /* Network initialization */
    wiz_toe_init();
    wiz_phy_link_check();

    network_init(g_ethernet_buf, &g_default_net_info);

    /* MQTT initialization */
    mqtt_init(SOCKET_ID, s_mqtt_send_buf, s_mqtt_recv_buf);

    /* Main loop */
    while (1)

    {
        // Check and execute the flashing light effect
        if (g_flowing_light == 1)
        {
            ws2812b_running_light();
        }
        // Check and execute the rainbow light effect
        if (g_rainbow_effect == 1)
        {
            ws2812b_rainbow_effect();
        }
        do_mqtt();
    }
}
